/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.example;

import jsc.datastructures.MatchedData;
import jsc.relatedsamples.TwoWayANOVA;

/**
 *
 * @author Nadeeshani Hewage
 * Undergraduate: University of Moratuwa, Sri Lanka
 */
public class TwoWayAnova {

    String trtLabels[];
    String blkLabels[];
    double observations[][];
    MatchedData core;
    TwoWayANOVA tester;
    String[][] table;

    double treatmentDOF,blockDOF, errorDOF, totalDOF;        // DOF stands for Degrees of Freedom
    double treatmentSS, blockSS, errorSS, totalSS;           // SS stands for Sum of Squares
    double treatmentMS, blockMS, errorMS;                    // MS stands for Mean Square
    double fRatioTr;// fRatioBlk;

    public TwoWayAnova(String[][] treatments, String[][] blocks, double[][] responseVariable){

        trtLabels=new String[treatments[0].length];         //creates and inserts cell data of treatment labels
                                                            //from the spreadsheet to the string array
            for(int i=0;i<treatments[0].length;i++){
            trtLabels[i]=treatments[0][i];
        }
        blkLabels=new String[blocks.length];                //creates and inserts cell data of block labels
                                                            //from the spreadsheet to the string array
            for(int i=0;i<blocks.length;i++){
            blkLabels[i]=blocks[i][0];
        }

        observations=responseVariable;

        core=new MatchedData(observations,blkLabels,trtLabels);     // instance of data structure for 2way anova
        tester=new TwoWayANOVA(core);                               //anova instance

    }
    public String[][] TwoWayTable(){                        //required values to create the Analysis of variance table

        treatmentDOF=(tester.getTreatmentCount()-1);
        blockDOF=(tester.getBlockCount()-1);
        errorDOF    =(treatmentDOF*blockDOF);
        totalDOF    =(tester.getN()-1);
        //double treatmentSS, errorSS, totalSS;
        treatmentSS =tester.getTreatmentEss();
        blockSS     =tester.getBlockEss();
        errorSS     =tester.getRss();
        totalSS     =tester.getTss();
        //double treatmentMS, errorMS;
        treatmentMS =tester.getTreatmentEms();
        blockMS     =tester.getBlockEms();
        errorMS     =tester.getRms();
        //fCalculated
        fRatioTr      =tester.getTestStatistic();
        //fRatioBlk     =tester.getTestStatistic();

        String StreatmentDOF,SblockDOF, SerrorDOF, StotalDOF;           // DOF stands for Degrees of Freedom
        String StreatmentSS,SblockSS, SerrorSS, StotalSS;               // SS stands for Sum of Squares
        String StreatmentMS,SblockMS, SerrorMS;                         // MS stands for Mean Square
        String SfRatioTr, statem;

        StreatmentDOF=""+treatmentDOF;                                  //string concatenation
        SblockDOF=""+blockDOF;
        SerrorDOF=""+errorDOF;
        StotalDOF=""+totalDOF;

        StreatmentSS=""+treatmentSS;
        SblockSS=""+blockSS;
        SerrorSS=""+errorSS;
        StotalSS=""+totalSS;

        StreatmentMS=""+treatmentMS;
        SblockMS=""+blockMS;
        SerrorMS=""+errorMS;

        SfRatioTr=""+fRatioTr;
        statem= "Your data supports null hypothesis upto "+tester.getSP()+"significance level";

        table=new String[][]{{"Null Hypothesis: Means of all treatments & blocks are equal"},   //output table
                             {"Source","DOF","SS","MS","F-ratio"},
                             {"Treatment",StreatmentDOF,StreatmentSS,StreatmentMS,SfRatioTr},
                             {"Block",SblockDOF,SblockSS,SblockMS,""},
                             {"Error",SerrorDOF,SerrorSS,SerrorMS,""},
                             {"Total",StotalDOF,StotalSS,"",""},
                             {statem}};

        return table;
    }

}
